package com.imooc.controller.center;

import com.imooc.controller.BaseController;
import com.imooc.enums.YesOrNo;
import com.imooc.pojo.OrderItems;
import com.imooc.pojo.Orders;
import com.imooc.pojo.bo.center.OrderItemsCommentBO;
import com.imooc.service.center.CenterCommentService;
import com.imooc.utils.IMOOCJSONResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author mark
 * @date 2021/4/16 0016 22:33
 */
@Api(value = "用户订单评价接口", tags = {"用户订单评价相关接口"})
@RestController
@RequestMapping({"/comment"})
public class CenterCommentsController extends BaseController {

    @Autowired
    private CenterCommentService centerCommentService;

    @ApiOperation(value = "保存评论列表", notes = "保存评论列表", httpMethod = "POST")
    @PostMapping("/saveList")
    public IMOOCJSONResult saveList(
            @ApiParam(name = "userId", value = "用户id", required = true)
            @RequestParam String userId,
            @ApiParam(name = "orderId", value = "订单id", required = true)
            @RequestParam String orderId,
            @RequestBody List<OrderItemsCommentBO> commentList) {

        // 判断用户和订单是否关联
        IMOOCJSONResult checkResult = checkUserOrder(userId, orderId);
        if (checkResult.getStatus() != HttpStatus.OK.value()) {
            return checkResult;
        }
        // 判断评论内容list不能为空
        if (commentList == null || commentList.isEmpty()) {
            return IMOOCJSONResult.errorMsg("评论内容不能为空！");
        }
        centerCommentService.batchSave(orderId, userId, commentList);
        return IMOOCJSONResult.ok();
    }

    @ApiOperation(value = "查询订单列表", notes = "查询订单列表", httpMethod = "POST")
    @PostMapping("/pending")
    public IMOOCJSONResult pending(
            @ApiParam(name = "userId", value = "用户id", required = true)
            @RequestParam String userId,
            @ApiParam(name = "orderId", value = "订单id", required = true)
            @RequestParam String orderId) {
        // 判断用户和订单是否关联
        IMOOCJSONResult checkResult = checkUserOrder(userId, orderId);
        if (checkResult.getStatus() != HttpStatus.OK.value()) {
            return checkResult;
        }
        // 判断该笔订单是否已经评价过，评价过了就不再继续
        Orders myOrder = (Orders) checkResult.getData();
        if (myOrder.getIsComment().equals(YesOrNo.YES.type)) {
            return IMOOCJSONResult.errorMsg("该笔订单已经评价");
        }
        List<OrderItems> list = centerCommentService.getPendingComments(orderId);

        return IMOOCJSONResult.ok(list);
    }

    @ApiOperation(value = "根据用户id和状态查询用户中心订单评价数据", notes = "根据用户id和状态查询用户中心订单评价数据", httpMethod = "GET")
    @GetMapping("/get-my-Comment")
    public IMOOCJSONResult getMyComment(@ApiParam(value = "用户id", name = "userId", required = true)
                                        @RequestParam(value = "userId") String userId,
                                        @ApiParam(name = "page", value = "第几页") @RequestParam Integer page,
                                        @ApiParam(name = "pageSize", value = "该页多少页数") @RequestParam Integer pageSize) {
        if (null == userId) {
            return IMOOCJSONResult.errorMsg("用户id不能为空");
        }
        if (null == page) {
            page = PAGE;
        }

        if (null == pageSize) {
            pageSize = COMMON_PAGE_SIZE;
        }

        return IMOOCJSONResult.ok(centerCommentService.getMyComment(userId, page, pageSize));
    }


}
